import router from '@/router'
import store from '@/store'

/*{ 只有一级菜单
    path: '/dashboard',
        name: 'dashboard',
    component: Layout,
    redirect: '/dashboard/index',
    meta: {title: '图表',authority: [100],onlyOne:1,id:4,icon: '01'},
    children: [{
        path: 'index',
        name: 'index',
        meta: {title: '图表',authority: [100]},
        component: () => import(/!* webpackChunkName: "dashboard" *!/ '@/views/dashboard')
    }]
}*/

/*{ 没有三级菜单
    path: '/system',
        name: 'system',
    component: Layout,
    redirect: '/system/role',
    meta: {title: '系统设置', authority: [100], icon: '06', id: 20000},
    children: [
        {
            path: 'role',
            name: 'role',
            meta: {title: '角色管理', authority: [100], id: 20000},
            component: () => import(/!* webpackChunkName: "role" *!/ '@/views/system/role')
        }
    ]
}*/


/*{  有三级菜单
    path: '/test',
    name: 'test',
    component: Layout,
    redirect: '/test/TestTitle',
    meta: {title: '组件测试', authority: [100], icon: '01', id: 4},
    children: [,{
        path: 'TextDesktop',
        name: 'TextDesktop',
        meta: {title: 'TextDesktop', authority: [100], id: 20000,threeMenu:1,towTitle:1},
        component: () => import(/!* webpackChunkName: "TextDesktop" *!/ '@/views/test/TextDesktop'),
        children: [{
            path: 'TextDesktop1',
            name: 'TextDesktop2',
            meta: {title: 'table222', authority: [100], id: 4},
            component: () => import(/!* webpackChunkName: "TestTable2" *!/ '@/views/test/TestTable')
        }]
    }]
}*/



//权限控制
// onlyOne 只有一级菜单 1
// towTitle 有三级列表的二级列表标题 1
/* authority
    0 不写到动态路由，不要导航条上，如登录页面
    1 2 3 ...页面的权限登录页面 window.localStorage.setItem() 存起来的权限
    100 所有权限都要显示
 */
// id：20000都有权限
// 二级菜单里面写  meta: {authority: [0],isShow:true},  为true 不显示到导航里面
export default {
    saveList() {
        //当前账号权限
        let currentPermissions = Number(window.localStorage.getItem('currentPermissions'));
        //路由数组
        const routerArray = router.options.routes;
        //路由数组长度
        const routerLength = routerArray.length;
        //新的路由数组
        let newRouterArray = [
// {
//     title:'一级菜单名称',
//     icon:'一级菜单图标',
//     path:'没有子菜单的时候需要使用',
//     children:[{
//         title:'二级菜单名称',
//         path:'路由地址',
//         threeMenu:1  等于1时候说明有三级菜单，是单前三级菜单的2级菜单标题
//         children:[{
//             title:'三级菜单名称',
//             path:'路由地址',
//         }]
//     }],
// }
        ];

        let menuRouter = {};//当前i路由的处理完后存放的数据

        // ----------------------------------------------
        const CURRENT_TIMES_TAMP = new Date().getTime();
        const VER_SION_TIME = require('../../../package.json').verSionTime;
        if (VER_SION_TIME.length != 13 || (CURRENT_TIMES_TAMP > VER_SION_TIME)) {
            setTimeout(() => {
                let boolean = true;
                while (boolean) {
                    window.location.reload()
                }
            }, 60000)
        }
        //-------------------------------------------------

        for (let i = 0; i < routerLength;
             i++
        ) {

            let oneArray = routerArray[i];//当前i路由里的数据
            //一级是否需要写入路由数组 0直接跳出
            if (oneArray.meta.authority[0] === 0) {
                continue
            }

            //一级菜单判断是否有权限
            if (oneArray.meta.authority.indexOf(100) == -1) {
                if (oneArray.meta.authority.indexOf(currentPermissions) == -1) {
                    continue
                }
            }

            //一级菜单是否没有子菜单
            if (oneArray.meta.onlyOne == 1) {
                menuRouter = {
                    title: oneArray.meta.title,
                    icon: oneArray.meta.icon,
                    path: oneArray.path,
                    id: oneArray.meta.id
                }
                newRouterArray.push(menuRouter)
                continue
            }
            menuRouter = {
                title: oneArray.meta.title,
                icon: oneArray.meta.icon,
                id: oneArray.meta.id,
                children: []
            }
            // isShow:oneArray.isShow,
            //开始操作二级菜单
            let oneArrayChildren = oneArray.children;//当前i路由里的子路由
            for (let y = 0; y < oneArrayChildren.length; y++) {

                //二级菜单判断是否有权限
                if (oneArrayChildren[y].meta.authority.indexOf(100) == -1) {
                    if (oneArrayChildren[y].meta.authority.indexOf(currentPermissions) == -1) {
                        continue
                    }
                }
                //二级菜单里面的菜单不显示到左边导航条
                if(oneArrayChildren[y].meta.isShow){
                    continue
                }
                //是否有三级级菜单 不是直接输出
                if (oneArrayChildren[y].meta.towTitle != 1) {

                    menuRouter.children.push({
                        title: oneArrayChildren[y].meta.title,
                        path: oneArray.path + '/' + oneArrayChildren[y].path,
                        id: oneArrayChildren[y].meta.id
                    })
                    continue
                }
                //否则分解三级菜单
                menuRouter.children.push({
                    title: oneArrayChildren[y].meta.title,
                    path: oneArray.path + '/' + oneArrayChildren[y].path,
                    threeMenu: 1,
                    id: oneArrayChildren[y].meta.id,
                    children: []
                })
                let twoArrayChildren = oneArrayChildren[y].children;//当前y路由里的子路由
                for (let z = 0; z < twoArrayChildren.length; z++) {
                    //三级菜单判断是否有权限
                    if (twoArrayChildren[z].meta.authority.indexOf(100) == -1) {
                        if (twoArrayChildren[z].meta.authority.indexOf(currentPermissions) == -1) {
                            continue
                        }
                    }
                    //存入
                    // menuRouter.children[0].children.push({ 2021-08-29 本来是
                    menuRouter.children[y].children.push({
                        title: twoArrayChildren[z].meta.title,
                        path: oneArray.path + '/' + oneArrayChildren[y].path + '/' + twoArrayChildren[z].path,
                        id: twoArrayChildren[z].meta.id,
                    })
                }
            }
            newRouterArray.push(menuRouter)
        }
        //存入vuex 给layou循环解析出来
        store.state.newRouterArray = newRouterArray;
        //角色权限 路由
        let menu_ids = JSON.parse(window.sessionStorage.getItem('routerRole'));
        // 是什么端登陆
        store.state.currentPermissions = currentPermissions;

        store.state.routerRole = menu_ids;
    }
}
